package controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import entity.Essay;
import service.FrameService;

/**
 * @author guojian
 *
 *
 */
@Controller
public class FrameController {

	@Resource(name = "frameServiceImpl")
	private FrameService frameService;

	/*@RequestMapping("about")
	public String AboutRequest() {
		return "about";
	}*/

	@RequestMapping("frame")
	public ModelAndView FrameRequest() {
		ModelAndView mv = new ModelAndView("frame");
		// 默认第0页开始
		int page = 0;
		List<Essay> essays = frameService.getPage(page);
		mv.addObject("page", page);
		mv.addObject("essays", essays);
		return mv;
	}

	@RequestMapping("previous_page")
	public ModelAndView previousPage(HttpServletRequest request) {

		int page = Integer.parseInt(request.getParameter("page"));
		if (page < 0) {
			return new ModelAndView("redirect:frame");
		} else {
			ModelAndView mv = new ModelAndView("frame");
			List<Essay> essays = frameService.getPage(page);
			mv.addObject("page", page);
			mv.addObject("essays", essays);
			return mv;
		}
	}

	@RequestMapping("next_page")
	public ModelAndView nextPage(HttpServletRequest request) {
		int page = Integer.parseInt(request.getParameter("page"));
		ModelAndView mv = new ModelAndView("frame");
		List<Essay> essays = frameService.getPage(page);
		if(essays.size()==0){
			essays=frameService.getPage(page-1);
			mv.addObject("page", page-1);
			mv.addObject("essays", essays);
			return mv;
		}else{
			mv.addObject("page", page);
			mv.addObject("essays", essays);
			return mv;
		}
		
	}

	public FrameService getFrameService() {
		return frameService;
	}

	public void setFrameService(FrameService frameService) {
		this.frameService = frameService;
	}

}
